﻿Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports WisSensorNLibLib


Public Class ModificarPersonas
    Inherits System.Web.UI.Page
    Private WithEvents msensor As WisSensorN

    Private Const MaxUser As Integer = 10
    Private WisObj As New WisSensorN
    Private DB As String() = New String(MaxUser - 1) {}
    Private nEnrolled As Integer = 0
    Private indx As String
    Private msg As String
    Private ClavePAraLoguearse As String

    Private Function validaDireccionDeMail() As Boolean

        If TxtMail.Text <> "" Then

            If InStr(TxtMail.Text, "@", CompareMethod.Text) = 0 Then
                validaDireccionDeMail = False
                Exit Function
            End If

            If InStr(TxtMail.Text, ".", CompareMethod.Text) = 0 Then
                validaDireccionDeMail = False
                Exit Function
            End If

            If InStr(TxtMail.Text, ".com", CompareMethod.Text) = 0 Then
                validaDireccionDeMail = False
                Exit Function
            End If


            ''si llego hasta aca es q ta tod bien 
            validaDireccionDeMail = True
        Else
            validaDireccionDeMail = False
        End If

    End Function


    Private Sub LimpiaCampos()

        TxtNombre.Text = ""
        TxtApellido.Text = ""
        txtDNI.Text = ""
        TxtFechaNac.Text = ""
        'MuestraCartel("Se guardaron los datos correctamente", Label1)


        Rb_Max.Checked = True

        TxtCalle.Text = ""
        txtnumero.Text = ""
        TXtPiso.Text = ""
        txtdpto.Text = ""

        TxtMail.Text = ""

        TxtCelular.Text = ""
        Txttelefono.Text = ""

        txtCodPostal.Text = ""

        chkGeneraDniProvisorio.Checked = False

        txtHuella.Text = ""

    End Sub

    Private Function RealizaCargaDeDatos() As Boolean
        Dim i As Integer = 17
        Dim param(i - 1) As ADODB.Parameter



        param(0) = New ADODB.Parameter
        param(0).Name = "@nombre"
        param(0).Type = ADODB.DataTypeEnum.adVarChar
        param(0).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(0).Value = TxtNombre.Text
        param(0).Size = 50

        param(1) = New ADODB.Parameter
        param(1).Name = "@apellido"
        param(1).Type = ADODB.DataTypeEnum.adVarChar
        param(1).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(1).Value = TxtApellido.Text
        param(1).Size = 50

        param(2) = New ADODB.Parameter
        param(2).Name = "@dni"
        param(2).Type = ADODB.DataTypeEnum.adVarChar
        param(2).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(2).Value = txtDNI.Text
        param(2).Size = 10

        param(3) = New ADODB.Parameter
        param(3).Name = "@FechNac"
        param(3).Type = ADODB.DataTypeEnum.adDate '<--campo fecha
        param(3).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(3).Value = TxtFechaNac.Text


        param(4) = New ADODB.Parameter
        param(4).Name = "@nacionalidad"
        param(4).Type = ADODB.DataTypeEnum.adVarChar
        param(4).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(4).Value = CboNacionalidad.Text
        param(4).Size = 50



        param(5) = New ADODB.Parameter
        param(5).Name = "@sexo"
        param(5).Type = ADODB.DataTypeEnum.adVarChar
        param(5).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(5).Value = IIf(Rb_Max.Checked = True, "M", "F")
        param(5).Size = 1

        param(6) = New ADODB.Parameter
        param(6).Name = "@tel"
        param(6).Type = ADODB.DataTypeEnum.adVarChar
        param(6).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(6).Value = Txttelefono.Text
        param(6).Size = 50

        param(7) = New ADODB.Parameter
        param(7).Name = "@celular"
        param(7).Type = ADODB.DataTypeEnum.adVarChar
        param(7).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(7).Value = TxtCelular.Text
        param(7).Size = 50

        param(8) = New ADODB.Parameter
        param(8).Name = "@huella"
        param(8).Type = ADODB.DataTypeEnum.adVarChar
        param(8).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(8).Value = txtHuella.Text
        param(8).Size = 8000

        param(9) = New ADODB.Parameter
        param(9).Name = "@direccion"
        param(9).Type = ADODB.DataTypeEnum.adVarChar
        param(9).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(9).Value = TxtCalle.Text
        param(9).Size = 50


        param(10) = New ADODB.Parameter
        param(10).Name = "@numero"
        param(10).Type = ADODB.DataTypeEnum.adVarChar
        param(10).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(10).Value = txtnumero.Text
        param(10).Size = 10

        param(11) = New ADODB.Parameter
        param(11).Name = "@provincia"
        param(11).Type = ADODB.DataTypeEnum.adVarChar
        param(11).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(11).Value = CboProvincia.Text
        param(11).Size = 50

        param(12) = New ADODB.Parameter
        param(12).Name = "@localidad"
        param(12).Type = ADODB.DataTypeEnum.adVarChar
        param(12).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(12).Value = CboLocalidad.Text
        param(12).Size = 50


        param(13) = New ADODB.Parameter
        param(13).Name = "@Piso"
        param(13).Type = ADODB.DataTypeEnum.adVarChar
        param(13).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(13).Value = TXtPiso.Text
        param(13).Size = 5

        param(14) = New ADODB.Parameter
        param(14).Name = "@dpto"
        param(14).Type = ADODB.DataTypeEnum.adVarChar
        param(14).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(14).Value = txtdpto.Text
        param(14).Size = 2

        param(15) = New ADODB.Parameter
        param(15).Name = "@codPostal"
        param(15).Type = ADODB.DataTypeEnum.adInteger
        param(15).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(15).Value = CInt(IIf(txtCodPostal.Text = "", "0", txtCodPostal.Text))



        param(16) = New ADODB.Parameter
        param(16).Name = "@Mail"
        param(16).Type = ADODB.DataTypeEnum.adVarChar
        param(16).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(16).Value = TxtMail.Text
        param(16).Size = 50

        RealizaCargaDeDatos = Datos.Modificar("ModificaPersonas_MD", param, "funcion q modifica personas")

        If RealizaCargaDeDatos Then
            LimpiaCampos()
            txtDNIBusqueda.Text = ""
        End If

    End Function

    Private Sub CargaDatos()

        FG.CargaCombo("traerProvincias", CboProvincia)
        If CboProvincia.Text <> "" Then
            FG.CargaCombo("TraeLocalidadxProvincia_MD", CboLocalidad, CboProvincia.Text)
        End If


        FG.CargaCombo("traerPaises", CboNacionalidad)
        CboNacionalidad.SelectedValue = UCase("Argentina")




    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                FG.culturaPaginas()

                CargaDatos()

                Var_Globales.Setea_paginas("AltaDePersonas.aspx")


            Else
                If Request.Form("__EVENTTARGET") = "FechaNacimiento" Then
                    TxtFechaNac.Text = Request.Form("__EVENTARGUMENT")
                End If
            End If
        Catch ex As Exception
        End Try


    End Sub


    Protected Sub CboProvincia_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CboProvincia.SelectedIndexChanged



        FG.CargaCombo("TraeLocalidadxProvincia_MD", CboLocalidad, CboProvincia.Text)



    End Sub



    Protected Sub cmdLimpiar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdLimpiar.Click


        LimpiaCampos()
    End Sub
    Private Function ValidaExistenciaDEhueya() As Boolean

        Dim param(0) As ADODB.Parameter
        Dim Rs As ADODB.Recordset



        param(0) = New ADODB.Parameter
        param(0).Name = "@Hueya"
        param(0).Type = ADODB.DataTypeEnum.adVarChar
        param(0).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(0).Value = txtHuella.Text
        param(0).Size = 8000

        Rs = Datos.TraerTodos("BuscaSiExiteUNaHuella", param, " validaExistenCiaDeDNI")

        ValidaExistenciaDEhueya = CBool(Rs.Fields(0).Value.ToString())



    End Function
    Private Function validaExistenCiaDeDNI() As Boolean
        Dim param(0) As ADODB.Parameter
        Dim Rs As ADODB.Recordset



        param(0) = New ADODB.Parameter
        param(0).Name = "@dni"
        param(0).Type = ADODB.DataTypeEnum.adVarChar
        param(0).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(0).Value = txtDNI.Text
        param(0).Size = 10

        Rs = Datos.TraerTodos("BuscaSiexisteUNDni_Md", param, " validaExistenCiaDeDNI")

        validaExistenCiaDeDNI = CBool(Rs.Fields(0).Value.ToString())






    End Function
    Private Function Asocia_huella() As Boolean

        Dim param(1) As ADODB.Parameter


        param(0) = New ADODB.Parameter
        param(0).Name = "@dni"
        param(0).Type = ADODB.DataTypeEnum.adVarChar
        param(0).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(0).Value = txtDNI.Text
        param(0).Size = 255

        param(1) = New ADODB.Parameter
        param(1).Name = "@huella"
        param(1).Type = ADODB.DataTypeEnum.adVarChar
        param(1).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(1).Value = txtHuella.Text
        param(1).Size = 8000


        Asocia_huella = Datos.Agregar("AsociaHuellaCnDni_MD", param, "funcion que Asocia la huella")






    End Function

    Protected Sub CmdGuardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CmdGuardar.Click
        Dim Resp As Single
        Dim FechaActual As Date = Date.Today


        'oculto el lblmsje de guardar
        FG.SacaCartelDeAViso(LblMsjeALGuardar)
        FG.SacaCartelDeAViso(lblMensajeRegistro)
        ''''''''''''''''''''''''''

        '----------------hacemos las validaciones--->
        If FG.ValNombres(TxtNombre.Text) = False Then
            FG.MuestraCartelDeguardar("No se permiten numeros en el campo Nombre", LblMsjeALGuardar)
            Exit Sub
        End If

        If FG.ValNombres(TxtApellido.Text) = False Then
            FG.MuestraCartelDeguardar("No se permiten numeros en el campo Apellido", LblMsjeALGuardar)
            Exit Sub
        End If

        If txtDNI.Enabled = True Then
            If FG.ValCampoNumericos(txtDNI.Text, "DNI") = False Then
                FG.MuestraCartelDeguardar("hay un caracter no valido en el campo DNI", LblMsjeALGuardar)
                Exit Sub
            End If
        End If

        If Mid(txtDNI.Text, 1, 1) = "0" Then
            FG.MuestraCartelDeguardar("El dni no puede comenzar con 0", LblMsjeALGuardar)
            Exit Sub
        End If


        If txtnumero.Text = "" Then
            Resp = MsgBox("No ha informado el numero de la direccion,esta seguro que desea continuar?", MsgBoxStyle.YesNo, "Falta numero de direccion")
            If Resp <> 6 Then
                Exit Sub
            End If
        End If


        If TxtMail.Text <> "" Then
            If validaDireccionDeMail() = False Then
                FG.MuestraCartelDeguardar("Formato invalido en la direccion de mail", LblMsjeALGuardar)
                Exit Sub
            End If
        End If

        If Len(TxtNombre.Text) < 3 Then
            FG.MuestraCartelDeguardar("No se especificado un nombre adecuado", LblMsjeALGuardar)
            Exit Sub
        End If

        If IsDate(TxtFechaNac.Text) = False Then
            FG.MuestraCartelDeguardar("El valor ingresado en el campo Fecha no es valido", LblMsjeALGuardar)
            Exit Sub

        Else
            If CDate(TxtFechaNac.Text) > Date.Today Then
                FG.MuestraCartelDeguardar("La fecha de nacimiento no puede ser superior a la actual", LblMsjeALGuardar)
                Exit Sub
            End If


        End If
        'este es el ultimo 

        '--------------------------------------------------




        If RealizaCargaDeDatos() = True Then

            'Enviar mail aca 
            Dim destinatario As String, asunto As String, cuerpomensaje As String

            destinatario = TxtMail.Text
            asunto = "Modificacion de Persona"
            cuerpomensaje = "<html><body><b>Se ha modificado usuario correctamente</b> <br/> " + _
                                    "<pre/><b>El equipo de SCHC.<pre/></b></body></html>"

            destinatario = FG.EnviarMail(destinatario, asunto, cuerpomensaje)

            If destinatario = "No hay mail registrado para notitificar el proceso" Then
                Label1.Text = Label1.Text + " pero, " + destinatario
            End If

            'popupConfirmacion.Show()
            MuestraCartel(Label1.Text, lblMensajeRegistro)
        Else
            MuestraCartel("se produjo un error al cargar en la base", LblMsjeALGuardar)

        End If

    End Sub


    Private Sub btnok_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnok.Click

        MsgBox("pepe")
    End Sub







    Private Function GenerarDniProvisorio() As String

        Dim Rs As ADODB.Recordset

        Rs = Datos.TraerTodos("GeneraDniProvisorio", Nothing, "alta persona- genearDniProvisorio")
        If Rs.RecordCount > 0 Then
            Rs.MoveFirst()
            GenerarDniProvisorio = Rs.Fields(0).Value.ToString

        End If

    End Function


    Protected Sub chkGeneraDniProvisorio_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkGeneraDniProvisorio.CheckedChanged

        If chkGeneraDniProvisorio.Checked = False Then
            txtDNI.Text = ""
            txtDNI.Enabled = True
        Else
            txtDNI.Text = GenerarDniProvisorio()
            txtDNI.Enabled = False
        End If

    End Sub

    Protected Sub Btn_Ok_UsuaYcontr_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Ok_UsuaYcontr.Click
        LimpiaCampos()
    End Sub





    Protected Sub Btn_OK_Verdadero_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_OK_Verdadero.Click

        Lector.CargaHuella_EN_TExt(txtHuella)
        mdlpopup.ViewStateMode = UI.ViewStateMode.Disabled

    End Sub

    Protected Sub btn_imprimir_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_imprimir.Click
        LimpiaCampos()
        Panel_conf_usuarioYcontr.Visible = False
    End Sub

    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscar.Click
        If txtDNIBusqueda.Text = "" Or txtDNIBusqueda.Text = "Búsqueda DNI" Then
            MuestraCartel("Para buscar ingrese DNI", lblMensajeRegistro)
            Exit Sub
        End If

        LimpiaCampos()
        BuscarDatos()


    End Sub

    Protected Sub BuscarDatos()
        Dim param(0) As ADODB.Parameter
        Dim Rs As ADODB.Recordset

        param(0) = New ADODB.Parameter
        param(0).Name = "@DNI"
        param(0).Type = ADODB.DataTypeEnum.adVarChar
        param(0).Direction = ADODB.ParameterDirectionEnum.adParamInput
        param(0).Value = txtDNIBusqueda.Text
        param(0).Size = 10

        SacaCartelDeAViso(lblMensajeRegistro)
        Rs = Datos.TraerTodos("traerDatosPersona_DNI", param, "Ventanilla.aspx")

        If Rs.RecordCount > 0 Then


            TxtNombre.Text = Rs.Fields(0).Value.ToString
            TxtApellido.Text = Rs.Fields(1).Value.ToString
            txtDNI.Text = Rs.Fields(2).Value.ToString
            TxtFechaNac.Text = Rs.Fields(3).Value.ToString
            CboNacionalidad.SelectedValue = UCase(Rs.Fields(4).Value.ToString)

            If Rs.Fields(5).Value.ToString = "M" Then
                Rb_Max.Checked = True
            Else
                Rb_fem.Checked = True
            End If
            TxtCalle.Text = Rs.Fields(6).Value.ToString
            txtnumero.Text = Rs.Fields(7).Value.ToString
            TXtPiso.Text = Rs.Fields(8).Value.ToString
            txtdpto.Text = Rs.Fields(9).Value.ToString
            CboProvincia.SelectedValue = UCase(Rs.Fields(10).Value.ToString)
            FG.CargaCombo("TraeLocalidadxProvincia_MD", CboLocalidad, CboProvincia.Text)

            CboLocalidad.SelectedValue = UCase(Rs.Fields(11).Value.ToString)
            txtCodPostal.Text = Rs.Fields(12).Value.ToString
            Txttelefono.Text = Rs.Fields(13).Value.ToString
            TxtCelular.Text = Rs.Fields(14).Value.ToString
            TxtMail.Text = Rs.Fields(15).Value.ToString

            If Not Rs.Fields(16).Value.ToString = "" Then
                txtHuella.Text = Rs.Fields(16).Value.ToString
            End If

        Else
            LimpiaCampos()
        End If

    End Sub


End Class
